package com.dragon.sdgp.util.defaultGenerate;

import com.dragon.sdgp.util.defaultGenerate.strategy.GenerateStrategy;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @AUTHOR : Dragon_Yang
 * @File : DefaultDataGenerateUtil
 * @DESCRIPTION :
 * @DATE : 2023/2/15 19:52
 */

/**
 * 默认词条生成工具，可生成：
 * 人名、中国省份、车牌、城市名、手机号码、性别、年龄
 */

public class DefaultDataGenerateUtil {

    private GenerateStrategy generateStrategy = null;

    private boolean allowRepeat = true;


    private DefaultDataGenerateUtil(GenerateStrategy generateStrategy, boolean allowRepeat){
        this.generateStrategy = generateStrategy;
        this.allowRepeat = allowRepeat;
    }

    private ArrayList<String> createList(int count){
        ArrayList<String> data = new ArrayList<>(count);
        HashMap<String, Integer> nameMap = new HashMap<>();
        for (int i = 0; i < count;) {
            String key = generateStrategy.createOne();
            //不允许重复进行去重
            if(!allowRepeat){
                //使用HashMap进行去重
                if(nameMap.containsKey(key)){
                    continue;
                }
                nameMap.put(key, 1);
            }
            data.add(key);
            i++;
        }
        return data;
    }

    public static ArrayList<String> getList(int count, GenerateStrategy generateStrategy, boolean allowRepeat){
        return new DefaultDataGenerateUtil(generateStrategy, allowRepeat).createList(count);
    }

    public static ArrayList<String> getList(int count, GenerateStrategy generateStrategy){
        return getList(count, generateStrategy, true);
    }

}


